package com.example.blog.config;

import com.example.blog.util.JWTutil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String cliams = request.getHeader("user_token");
        log.info("从request的header中获取信息{}",cliams);
        Claims verify = JWTutil.verify(cliams);
        if(verify==null){
            log.error("获取的JWT不正确");
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
